<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 栏目分类 控制器
 */
class Category extends Admin_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->service('doc/category_service');
        log_message('debug', 'Category Admin Controller Class Initialized');
    }

    /**
     * 列表页面
     */
    public function index() {
        if (IS_POST) {
            $posts = $this->input->post(null, true);
            foreach ($posts['data'] as $id => $post) {
                $save = array();
                $save['vieworder'] = (int)$post['vieworder'];
                //由于模型中使用了更新时触发事件，这里临时使用CI_Model模型
                //$this->category_model->update($id, $save);
                $this->db->update($this->category_model->table(), $save, array('id' => $id));
            }
            $this->show_success('success', 'admin/category/index');
        } else {
            $data = array();
            $data['title'] = '栏目分类';
            $data['cat_tree'] = $this->category_service->get_tree();
            $this->load->view('category/category_index', $data);
        }
    }

    /**
     * 添加页面
     */
    public function add() {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('title', '栏目名称', 'required');
        $this->form_validation->set_rules('name', '栏目标识', 'alpha_dash|is_unique[post_category.name]');
        $this->form_validation->set_rules('status', '栏目状态', 'required');
        if ($this->form_validation->run() == true) {
            $post = $this->input->post(null, true); //var_dump($post);

            //允许发布类型排序
            //asort($post['settings']['[post_typeorder']);

            $save = array();
            $save['pid'] = (int)$post['pid'];
            $save['title'] = $post['title'];
            $save['name'] = $post['name'];
            $save['link'] = $post['link'];
            $save['name'] = $post['name'];
            $save['vieworder'] = (int)$post['vieworder'];
            $save['status'] = isset($post['status']) ? (int)$post['status'] : 0;
            $save['settings'] = $post['settings'];
            $this->category_model->insert($save);
            $this->show_success('success', 'admin/category/index');
        } else {
            if ($error = $this->form_validation->get_error()) $this->show_error($error);

            $this->load->service('doc/doc_type_service');

            $data = array();
            $data['title'] = '栏目分类';
            $data['cat_dropdown'] = $this->category_service->get_dropdown();
            $data['doc_type'] = $this->doc_type_service->get_type();
            $this->load->view('category/category_add', $data);
        }
    }

    /**
     * 修改页面
     */
    public function edit() {
        $id = $this->input->get_post('id', true);
        $cat = $this->category_model->get($id);
        if (!$cat) $this->show_error('illegal_request');

        $this->load->library('form_validation');
        $this->form_validation->set_rules('title', '栏目名称', 'required');
        if ($cat['name'] != $this->input->post('name', true)) {
            $this->form_validation->set_rules('name', '栏目标识', 'alpha_dash|is_unique[post_category.name]');
        }
        $this->form_validation->set_rules('status', '栏目状态', 'required');
        if ($this->form_validation->run() == true) {
            $post = $this->input->post(null, true);

            //允许移动的上级栏目判断
            if (!in_array($post['pid'], $this->category_service->get_pids_allow_move($id))) {
                $this->show_error('亲，不能移动到自己的子分类下面哦');
            }

            $save = array();
            $save['pid'] = (int)$post['pid'];
            $save['title'] = $post['title'];
            $save['name'] = $post['name'];
            $save['link'] = $post['link'];
            $save['name'] = $post['name'];
            $save['vieworder'] = (int)$post['vieworder'];
            $save['status'] = isset($post['status']) ? (int)$post['status'] : 0;
            $save['settings'] = array_merge($this->category_model->get_default_settings(), $post['settings']);
            $this->category_model->update($id, $save);
            $this->show_success('success', 'admin/category/index');
        } else {
            if ($error = $this->form_validation->get_error()) $this->show_error($error);

            $this->load->service('doc/doc_type_service');

            //允许发布类型排序
            asort($cat['settings']['post_typeorder']);
            //扩展配置信息默认值
            $cat['settings'] = array_merge($this->category_model->get_default_settings(), (array)$cat['settings']);

            $data = array();
            $data['title'] = '栏目分类';
            $data['cat'] = $cat;
            $data['cat_dropdown'] = $this->category_service->get_dropdown_allow_move($cat['pid'], $id);
            $data['doc_type'] = $this->doc_type_service->get_type();
            $this->load->view('category/category_edit', $data);
        }
    }

    /**
     * 删除处理
     */
    public function dodel() {
        $id = $this->input->get_post('id', true);
        if (empty($id)) $this->show_error('illegal_request');

        if ($this->category_model->get_by('pid', $id)) {
            $this->show_error('有子分类不能删除');
        }

        //TODO:判断栏目是否有文章，有文章也不允许删除

        $this->category_model->delete($id);
        $this->show_success('success', 'admin/category/index');
    }
}

/* End of file admin/category.php */
/* Location: ./application/controllers/admin/category.php */
